package com.view.validator
{

	public class StringTrimmer
	{
		private var stringField:Object;
		private var strFieldToStr:String;
		private var trim:Boolean;

		public function StringTrimmer()
		{

		}

		public function doValidation(item:Object, trimItem:Boolean=true):void
		{
			stringField=item;
			strFieldToStr=stringField.text;
			trim=trimItem;
			var stringID:*=stringField.id;
			strFieldToStr=trimSpaces(stringField.id, strFieldToStr)
			stringField.text=strFieldToStr;
		}

		/************************************************************/
		private function trimSpaces(myID:String, myItem:String):String
		{
			var tmp:String="";
			var myItem_length:int=myItem.length;
			var length_minus_1:int=myItem.length - 1;
			for (var index:int=0; index < myItem_length; index++)
			{
				if (myItem.charAt(index) != ' ')
				{
					tmp+=myItem.charAt(index);
				}
				else
				{
					if (tmp.length > 0)
					{
						if (myItem.charAt(index + 1) != ' ' && index != length_minus_1)
						{
							tmp+=myItem.charAt(index);
						}
					}
				}
			}
			//
			myItem=tmp;
			myItem_length=myItem.length
			var tempo:String="";
			for (index=0; index < myItem_length; index++)
			{
				if (myItem.charAt(index) != '-' && myItem.charAt(index) != ' ')
				{
					tempo+=myItem.charAt(index);
				}
				else
				{
					if (tempo.length > 0)
					{
						if (myItem.charAt(index) == '-' && index != length_minus_1 && index != 0)
						{
							tempo+=myItem.charAt(index);
						}
						else if (myItem.charAt(index) == ' ' && myItem.charAt(index + 1) == '-' && index != length_minus_1 && index != 0)
						{
							tempo+=myItem.charAt(index + 1);
						}
						else if (myItem.charAt(index) == ' ' && myItem.charAt(index - 1) == '-' && index != length_minus_1 && index != 0)
						{
							tempo+=myItem.charAt(index + 1);
						}
						else
						{
							tempo+=myItem.charAt(index);
						}
					}
				}
			}
			myItem=tempo;
			myItem_length=myItem.length
			var temp:String="";
			for (index=0; index < myItem_length; index++)
			{
				if (myItem.charAt(index) != '-')
				{
					temp+=myItem.charAt(index);
				}
				else
				{
					if (temp.length > 0)
					{
						if (myItem.charAt(index + 1) != '-' && index != length_minus_1)
						{
							temp+=myItem.charAt(index);
						}
					}
				}
			}
			var tmpT:Object=[];
			tmpT=temp.split(" ");
			var upTempor:Object=[];
			for (var j:int=0; j < tmpT.length; j++)
			{
				var upTmp:String=tmpT[j];
				var firstChar:String=upTmp.substring(0, 1);
				firstChar=firstChar.toUpperCase();
				upTempor[j]=firstChar + upTmp.substring(1, upTmp.length);
			}
			for (j=0; j < upTempor.length; j++)
			{
				if (upTempor[j].indexOf("-") != -1)
				{
					var splitScore:Object=upTempor[j].split("-");
					var Tempor:String="";
					for (var k:int=0; k < splitScore.length; k++)
					{
						var FirstChar:String=splitScore[k].substr(0, 1);
						var UpFirstChar:String=FirstChar.toUpperCase();
						if (k < splitScore.length - 1)
						{
							Tempor+=UpFirstChar + splitScore[k].substr(1) + "-";
						}
						else
						{
							Tempor+=UpFirstChar + splitScore[k].substr(1);
						}
					}
					upTempor[j]=Tempor;
				}
			}
			var myfinalItem:String="";
			for (j=0; j < upTempor.length; j++)
			{
				myfinalItem+=upTempor[j] + " ";
			}
			//
			if (trim == false)
			{
				myfinalItem=myfinalItem.toLowerCase();
			}
			while (myfinalItem.charAt(myfinalItem.length - 1) == ' ')
			{
				myfinalItem=myfinalItem.substring(0, myfinalItem.length - 1);
				while (myfinalItem.charAt(myfinalItem.length - 1) == '-')
				{
					myfinalItem=myfinalItem.substring(0, myfinalItem.length - 1);
				}
			}
			while (myfinalItem.charAt(0) == ' ')
			{
				myfinalItem=myfinalItem.substring(1);
				while (myfinalItem.charAt(0) == '-')
				{
					myfinalItem=myfinalItem.substring(1);
				}
			}
			//
			tmp="";
			myItem=myfinalItem;
			myItem_length=myItem.length;
			length_minus_1=myItem.length - 1;
			for (index=0; index < myItem_length; index++)
			{
				if (myItem.charAt(index) != ' ')
				{
					tmp+=myItem.charAt(index);
				}
				else
				{
					if (tmp.length > 0)
					{
						if (myItem.charAt(index + 1) != ' ' && index != length_minus_1)
						{
							tmp+=myItem.charAt(index);
						}
					}
				}
			}
			myfinalItem=tmp;
			return myfinalItem;
		} //end function trimSpaces(myID,myItem)
	} //doValidation
} //StringValidator